package com.example.springboot_crud.mapper;

import com.example.springboot_crud.pojo.Emp;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface EmpMapper {


    // 根据ID删除数据
    @Delete("delete from emp where id = #{id}")
    void delete(Integer id);

    // 新增员工操作
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time) " +
            "values (#{username},#{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime})")
    void insert(Emp emp);

    // 更新数据
    @Update("update emp set username = #{username}, name = #{name}, gender = #{gender}, image = #{image}, job = #{job}, entrydate = #{entrydate}, dept_id = #{deptId},update_time = #{updateTime}"+
            "where  id = #{id};")
    void update(Emp emp);


    // 根据id查询员工数据
//    @Select("select * from emp where id = #{id}")
//    Emp getById(Integer id);

    // 方案1 ；起别名
//    @Select("select id, username, password, name, gender, image, job, entrydate, " +
//            "dept_id deptId, create_time createTime, update_time updateTime from emp where id = #{id}")
//    Emp getById(Integer id);

    // 方案2 ：手动映射封装
//    @Results({
//            @Result(column = "dept_id", property = "deptId"),
//            @Result(column = "create_time", property = "createTime"),
//            @Result(column = "update_time", property = "updateTime")
//    })
//    @Select("select * from emp where id = #{id}")
//    Emp getById(Integer id);

    // 方案3 ： mybatis驼峰命名 resources中配置


    // 条件查询
//    @Select("select * from emp where name like '%${name}%' and gender = #{gender} and " +
//            "entrydate between #{begin} and #{end} order by update_time desc ")
//    List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);


    // 动态条件查询
    List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);

    // 动态更新数据

    void update2(Emp emp);

    // foreach循环删除数据
    void deleteByIds(List<Integer> ids);
}
